<template>
    <div class="login">
        <div class="box">
            <div class="pre-box">
                <h1>WELCOME</h1>
                <p>JOIN US!</p>
                <div class="img-box">
                    <img src="../assets/slides/萝卜团.jpg" alt="">
                </div>
            </div>
            <div class="register-form">
                <div class="title-box">
                    <h1>注册</h1>
                </div>
                <div class="input-box">
                    <input type="text" placeholder="用户名" v-model="Rform.Rusername">
                    <input type="password" placeholder="密码" v-model="Rform.Rpassword">
                    <input type="password" placeholder="确认密码" v-model="Rform.Rrepassword">
                </div>
                <div class="btn-box">
                    <button @click="onRegister">注册</button>
                    <p @click="mySwitch">已有账号去登录</p>
                </div>
            </div>
            <div class="login-form">
                <div class="title-box">
                    <h1>登录</h1>
                </div>
                <div class="input-box">
                    <input type="text" placeholder="用户名" v-model="Lform.Lusername">
                    <input type="password" placeholder="密码" v-model="Lform.Lpassword">
                </div>
                <div class="btn-box">
                    <button @click="onLogin">登录</button>
                    <p @click="mySwitch">没有账号去注册</p>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup>
import { useCookies } from '@vueuse/integrations/useCookies';
import { useStore } from 'vuex';
import { ref, reactive } from 'vue';
import { login, register} from '~/api/manager';
import { ElNotification } from 'element-plus';
import { useRouter } from 'vue-router'

const router = useRouter();
const cookies = useCookies();
const store = useStore();
let flag = ref(true);
const Rform = reactive({
    Rusername: '',
    Rpassword: '',
    Rrepassword: ''
});
const Lform = reactive({
    Lusername: '',
    Lpassword: ''
});

const onRegister = () => {
    // console.log(Rform.Rusername, Rform.Rpassword, Rform.Rrepassword)
    register(Rform.Rusername, Rform.Rpassword, Rform.Rrepassword)
        .then((res) => {
            console.log(res);
            //提示成功
            ElNotification({
                message: res.message || '注册成功',
                type: 'success',
                duration: 3000
            })
            // 跳转登录页面
            mySwitch();
        })
};
const onLogin = () => {
    login(Lform.Lusername, Lform.Lpassword)
        .then((res) => {
            console.log(res);
            //提示成功
            ElNotification({
                message: res.message || '登录成功',
                type: 'success',
                duration: 3000
            })

            // 存储 token 和用户信息
            cookies.set("user-token", res.token)
            cookies.set("use_id", res.user.id)
           
            // 跳转到首页
            router.push({ name: 'index' });
        })
};
const mySwitch = () => {
    if (flag.value) {
        document.querySelector('.pre-box').style.transform = 'translateX(100%)';
        document.querySelector('.pre-box').style.backgroundColor = '#b0cfe9';
        document.querySelector('.img-box img').src = '/src/assets/slides/白菜水饺.jpg';
    } else {
        document.querySelector('.pre-box').style.transform = 'translateX(0%)';
        document.querySelector('.pre-box').style.backgroundColor = '#edd4dc';
        document.querySelector('.img-box img').src = ' /src/assets/slides/萝卜团.jpg';
    }
    flag.value = !flag.value;
};
</script>
<style scoped>
* {
    margin: 0%;
    padding: 0;
    box-sizing: border-box;
}

input {
    outline: none;
}


.login {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(to right, rgb(247, 209, 215), rgb(191, 227, 241));
}

.box {
    width: 1050px;
    height: 600px;
    position: relative;
    display: flex;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.6);
    box-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
}

.pre-box {
    width: 525px;
    height: 100%;
    border-radius: 4px;
    background-color: #edd4dc;
    box-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
    left: 0;
    top: 0;
    position: absolute;
    transition: 0.5s ease-in-out;
}

.pre-box h1 {
    color: white;
    font-size: 32px;
    font-weight: bold;
    margin-top: 150px;
    text-align: center;
    letter-spacing: 5px;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
}

.pre-box p {
    color: white;
    font-weight: bold;
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin: 20px 0;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
}

.img-box {
    width: 200px;
    height: 200px;
    margin: 20px auto;
    border-radius: 50%;
    box-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.img-box img {
    width: 100%;
    transition: 0.5s;
}

.login-form,
.register-form {
    flex: 1;
    height: 100%;
}

.title-box {
    height: 300px;
    line-height: 500px;
}

.title-box h1 {
    font-weight: bold;
    font-size: 32px;
    color: white;
    text-align: center;
    letter-spacing: 5px;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, 0.1);
}

.input-box {
    display: flex;
    flex-direction: column;
    align-items: center;
}

input {
    width: 60%;
    height: 40px;
    margin-bottom: 20px;
    text-indent: 4px;
    border: 1px solid #b0cfe9;
    border-radius: 4px;
}

input:focus {
    color: #b0cfe9;
}

input:focus::placeholder {
    opacity: 0;
}

.btn-box {
    display: flex;
    justify-content: center;
}

button {
    width: 100px;
    height: 30px;
    margin: 0 7px;
    line-height: 30px;
    border: none;
    border-radius: 4px;
    background-color: #69b3f0;
    color: white;
}

button:hover {
    cursor: pointer;
    opacity: 0.8;
}

.btn-box {
    height: 30px;
    font-size: 14px;
    line-height: 30px;
    color: white;
}

.btn-box p:hover {
    cursor: pointer;
    border-bottom: 1px solid white;
}
</style>